import * as moment from 'moment'
import { toRaw, unref } from 'vue'
import { isObject } from '../../../utils/validate'

/**
 * 给常驻病历和中医病历做一个统一的区分，暂时只有中医病历。。
 * 后期需求增加可以针对 section做一个动态匹配分组（病历+诊断）
 * @returns
 */
export function useEmrData() {
  const diagnostics = '诊断'
  const cnSection = 'CN1'
  const riskSeciton = 'RS1'
  // 复诊
  const reviewSection = '复诊'
  function initEmrData(data = [], isRisk) {
    return {
      emr: filterEmr(data, isRisk),
      emrDiagnostics: filterDiagnostics(data),
      cnEmr: filterCNEmr(data),
      cnDiagnostics: filterCNDiagnostics(data),
      riskDiagnostics: filterRiskDiagnostics(data),
    }
  }
  /**
   * 筛出固定的诊断
   * @param {*} data
   * @returns
   */
  function filterDiagnostics(data) {
    return data.filter((item) => {
      return (
        item.model === diagnostics &&
        item.section !== cnSection &&
        item.section !== riskSeciton
      )
    })
  }
  /**
   * 筛出固定的病历
   * @param {*} data
   * @returns
   */
  function filterEmr(data, isRisk) {
    return data.filter((item) => {
      console.log('risk = ' + isRisk)
      console.log('model = ' + item.model)

      // 统一不需要显示复诊模块， 复诊转移到治疗方案中
      // return (
      //   item.model !== diagnostics &&
      //   item.section !== cnSection &&
      //   item.model !== reviewSection
      // );

      if (isRisk) {
        return (
          item.model !== diagnostics &&
          item.section !== cnSection &&
          item.model !== reviewSection
        )
      } else {
        // debugger
        return item.model !== diagnostics && item.section !== cnSection
      }
    })
  }
  /**
   * 筛出中医诊断
   * @param {*} data
   */
  function filterCNDiagnostics(data) {
    return data.filter((item) => {
      return (
        item.model === diagnostics &&
        item.section === cnSection &&
        item.title === '中医辨证'
      )
    })
  }
  /**
   * 筛出中医病历
   * @param {*} data
   */
  function filterCNEmr(data) {
    return data.filter((item) => {
      return (
        item.model !== diagnostics &&
        item.section === cnSection &&
        item.title === '中医辨证'
      )
    })
  }
  /**
   * 筛出风险筛查
   * @param {*} data
   */
  function filterRiskDiagnostics(data) {
    return data.filter((item) => {
      return (
        item.model === diagnostics &&
        item.section === riskSeciton &&
        item.title === '风险筛查'
      )
    })
  }

  function diffDiagnostic(
    tmpDiagnostics = [],
    tmpAssessment = [],
    handDiagnostic,
    ageType
  ) {
    const diagnostics = tmpDiagnostics[0].data
    const assessment = tmpAssessment[0].data
    const acuteWithAN = 'attr318'
    const acuteWithANWith = 'attr319'
    const controlWithAN = 'attr317'
    let controlItem = null
    let acuteItem = null
    let controlIndex = null
    let acuteIndex = null
    console.log('ageType =' + ageType)
    assessment.forEach((item) => {
      if (item.an === acuteWithAN && ageType === 'lessThan') {
        console.log('item.an =' + 'acuteWithAN')
        acuteItem = item
      } else if (item.an === controlWithAN) {
        console.log('item.an =' + 'controlWithAN')
        controlItem = item
      } else if (item.an === acuteWithANWith && ageType === 'greaterEqual') {
        console.log('item.an =' + 'acuteWithANWith')
        acuteItem = item
      }
    })
    console.log('diagnostics =' + JSON.stringify(diagnostics))
    diagnostics.map((item, index) => {
      if (item.an === acuteWithAN && ageType === 'lessThan') {
        acuteIndex = index
      } else if (item.an === controlWithAN) {
        controlIndex = index
      } else if (item.an === acuteWithANWith && ageType === 'greaterEqual') {
        acuteIndex = index
      }
      const value = handDiagnostic[item.an]
      if (value) {
        item.b.ip = Array.isArray(value) ? value.join(',') : value
      }

      // if (value) {
      //   item.b.ip = value
      // }
    })
    console.log('after diagnostics =' + JSON.stringify(diagnostics))
    if (controlItem && controlIndex) {
      diagnostics.splice(controlIndex, 1, controlItem)
    }
    if (acuteItem && acuteIndex) {
      diagnostics.splice(acuteIndex, 1, acuteItem)
    }
    return diagnostics
  }
  /**
   * 过滤用户选择的值中的复诊时间
   * @param {*} data
   */
  function filterFollowDate(data = []) {
    console.log('data =' + JSON.stringify(data))
    // 时间选择器的attr值
    const attrID = 'attr520'
    const followDate = data.filter((item) => item.name === attrID)
    if (followDate && followDate.length > 0)
      return moment(followDate[0].attrValue).format('YYYY-MM-DD')
    return null
  }

  function filterReview(data = []) {
    return data.filter((item) => {
      return item.model === reviewSection
    })
  }
  return {
    initEmrData,
    diffDiagnostic,
    filterFollowDate,
    filterReview,
  }
}
